package Exercise;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class UDPReceiveExercise02 {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(8888);
        byte data[] = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);
        socket.receive(packet);
        int length = packet.getLength();
        byte[] data1 = packet.getData();
        String s = new String(data1, 0, length);
        String answer = "";
        if (s.equals("四大名著是哪些")) {
            answer = "《红楼梦》，《三国演义》，《水浒传》，《西游记》";
        } else {
            answer = "你在狗叫什么?";
        }
        byte[] bytes = answer.getBytes();
        packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8887);
        socket.send(packet);

        socket.close();
    }
}
